#!/bin/sh

# Slackware build script for sms_sdl
# Written by B. Watson (yalhcru@gmail.com)

PRGNAM=sms_sdl
VERSION=${VERSION:-0.9.4a_r7.1}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

SRCVER=$(echo $VERSION | sed 's/_/-/')

# If $MIME_TYPES is set to `yes' it will include MIME types for KDE and  
# automagically associate *.sms and *.gg files with sms_sdl. 
MIME_TYPES=${MIME_TYPES:-"yes"}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  cat <<EOF
This package can't easily be built on x86_64. Build the 32-bit
package on a 32-bit Slackware system. It should install and run
fine on a multilib x86_64 system such as slamd64, provided you
have 32-bit SDL libraries available.
EOF
exit 1
fi
  
set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$SRCVER
unzip $CWD/$PRGNAM-$SRCVER-src.zip
cd $PRGNAM-$SRCVER
chown -R root:root .
chmod -R a-s,u+w,go+r-w .

patch -p1 --verbose < $CWD/cflags_and_libz.diff

# Thanks to Ellington Santos (necropresto) for this patch that makes
# the --fm and --japan options actually work:
patch -p1 --verbose < $CWD/japan_and_fm.diff

cd sdl
make OPTFLAGS="$SLKCFLAGS" LDFLAGS=$SLKLDFLAGS

# There's no 'make install'
strip $PRGNAM
mkdir -p $PKG/usr/bin
install -m0755 $PRGNAM $PKG/usr/bin/$PRGNAM

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp README.TXT $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# man page is specific to this SlackBuild. If you're packaging for
# some other distribution, feel free to snag it.
mkdir -p $PKG/usr/man/man1
gzip -9c $CWD/$PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

# Desktop integration stuff: Icon and .desktop file always present,
# regardless of $MIME_TYPES
mkdir -p $PKG/usr/share/{applications,pixmaps}
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop
( cd $CWD
  for i in *.png; do
      cat $i > $PKG/usr/share/pixmaps/$i
  done
)

if [ "$MIME_TYPES" = "yes" ]; then
    mkdir -p $PKG/usr/share/mime/packages
    cat $CWD/$PRGNAM.xml > $PKG/usr/share/mime/packages/$PRGNAM.xml

    # To make KDE able to use the .desktop and MIME stuff properly, we need
    # to define each MIME type in /usr/share/mimelnk. This seems redundant to
    # me, but that's how it works...
    mkdir -p $PKG/usr/share/mimelnk/application
    ( cd $CWD
      for i in x-*.desktop; do
          cat $i > $PKG/usr/share/mimelnk/application/$i
      done
    )
fi

mkdir -p $PKG/install
cat $CWD/doinst.sh > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
